<template>
  <div class="dashboard-container">
    <div class="bgc">
      <div
        class="title"
        style="
          width: 100%;
          color: #fff;
          font-weight: 500;
          font-size: 24px;

          box-sizing: border-box;
          padding-left: 20px;
          display: flex;
          align-items: center;
          margin-top: 20px;
          margin-bottom: 30px;
        "
      >
        添加/编辑人员
      </div>
      <!-- <el-steps :active="type" style="width: 500px; margin: 0 auto">
        <el-step title="基础配置"></el-step>
        <el-step title="高级配置"></el-step>
        <el-step title="预警配置"></el-step>
        <el-step title="完成"></el-step>
      </el-steps> -->
      <div v-if="type == 1" class="forms">
        <el-form :model="form" :rules="rules" ref="form" label-width="100px" class="demo-ruleForm">
          <!-- <el-form-item label="项目id" prop="name">
            <el-input class="el-inputs" v-model="form.project_id"></el-input>
          </el-form-item> -->
          <el-form-item label="用户名" prop="">
            <el-input class="el-inputs" v-model="form.username"></el-input>
          </el-form-item>
          <el-form-item label="昵称：" prop="">
            <el-input class="el-inputs" v-model="form.nickname"></el-input>
          </el-form-item>
          <el-form-item label="密码：" prop="">
            <el-input class="el-inputs" v-model="form.password"></el-input>
          </el-form-item>
          <el-form-item label="公司Logo" prop="">
            <el-upload
              :action="uploadAction"
              :limit="1"
              :file-list="logoList"
              :on-success="uploadLogoSuccess"
              :on-progress="logoProcess"
              list-type="picture-card"
              :on-preview="handlePictureCardPreview"
              :on-remove="handleRemove"
              :class="{ disabled: uploadDisabled }"
            >
              <i class="el-icon-plus" />
            </el-upload>
            <el-dialog :visible.sync="dialogVisible">
              <img width="100%" :src="dialogImageUrl" alt="" />
            </el-dialog>
          </el-form-item>
          <el-form-item label="角色id：" prop="">
            <el-select v-model="form.role_id" placeholder="请选择角色">
              <el-option label="Administrator" value="1"></el-option>
              <el-option label="User" value="2"></el-option>
              <el-option label="Guest" value="3"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item class="" style="display: flex; justify-content: center">
            <el-button type="primary" style="margin: 0 auto" @click="submitForm('form')"
              >提交</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
  <script>
import { upload, admin_edit } from '@/api/user'
import { mapGetters } from 'vuex'

export default {
  computed: {
    ...mapGetters(['currentProjectId'])
  },
  watch: {
    currentProjectId(newValue, oldValue) {
      this.form.project_id = newValue
      // if (newValue !== oldValue) {
      //   this.form.project_id = newValue
      // }
    }
  },
  data() {
    return {
      info: {},
      type: 1,
      form: {
        id: Number,
        project_id: '',
        username: '',
        nickname: '',
        password: '',
        avatar: '',
        role_id: ''
      },
      logoList: [],
      logoLogoList: [],

      imageUrl: '',
      rules: {
        // name: [{ required: true, message: '请输入活动名称', trigger: 'blur' }]
        // region: [{ required: true, message: '请选择活动区域', trigger: 'change' }],
        // date1: [{ type: 'date', required: true, message: '请选择日期', trigger: 'change' }],
        // date2: [{ type: 'date', required: true, message: '请选择时间', trigger: 'change' }],
        // type: [
        //   { type: 'array', required: true, message: '请至少选择一个活动性质', trigger: 'change' }
        // ],
        // resource: [{ required: true, message: '请选择活动资源', trigger: 'change' }],
        // desc: [{ required: true, message: '请填写活动形式', trigger: 'blur' }]
      },
      uploadAction: process.env.VUE_APP_BASE_API + '/admin/upload'
    }
  },
  created() {
    // this.optionsValue = this.options[0]
    if (JSON.parse(this.$route.query.item)) {
      let tom = JSON.parse(this.$route.query.item)
      this.form = tom
    }
  },
  methods: {
    handleRemove(file, fileList) {
      console.log(file, fileList)
      this.dialogVisible = false
    },
    handlePictureCardPreview(file) {
      this.uploadDisabled = true
      this.dialogVisible = true
      // this.dialogImageUrl = file.url
    },
    logoProcess() {
      this.uploadDisabled = true
    },
    uploadLogoSuccess(response, file, fileList) {
      //
      if (response.code === 200) {
        this.form.avatar = response.data.path
        this.dialogImageUrl = process.env.VUE_APP_BASE_API + response.data.path
      }
    },
    customRequest({ file, onSuccess, onError, onProgress }) {
      upload({ file: file }).then(res => {
        console.log(res, '-----------')
      })
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          console.log(this.form, 'this.form')
          if (this.form.id) {
            this.form.id = Number(this.form.id)
          }
          this.form.project_id = Number(this.form.project_id)

          this.form.role_id = Number(this.form.role_id)
          admin_edit(this.form).then(res => {
            if (res.code === 200) {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
            }
            console.log(res, 'res')
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // submitForms(formName) {
    //   this.$refs[formName].validate(valid => {
    //     if (valid) {
    //       sensor_edit(this.form).then(res => {
    //         // console.log(res, '--111111')
    //         if (res.code === 200) {
    //           this.$message({
    //             message: '成功',
    //             type: 'success'
    //           })
    //           this.type = this.type + 1

    //           setTimeout(() => {
    //             this.$router.push({ path: 'equipmentManagement' })
    //           }, 2000)
    //         }
    //       })
    //     } else {
    //       console.log('error submit!!')
    //       return false
    //     }
    //   })
    // },
    out() {
      this.type = this.type - 1
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
  
  <style lang="scss" scoped>
.bgc {
  width: 90%;
  margin: 0 auto;
  height: 80vh;
  border: 1px solid rgb(101, 166, 231);
  // background-color: red;
  //   width: 400px;
  //  height: 300px;
  //  background-color: #222 !important;
  //  border-radius: 10px;
  //  display: flex;
  //  align-items: center;
  //  justify-content: center;
  //  font-size: 20px;
  //  color: #fff;
  //  font-weight: 800;
  //  position: relative;
  //  z-index: 20;
  // background: url('../../views/images/编组-3-20240612172302722_副本.png');
  background-size: 100% 100%;
  background-repeat: no-repeat;
  // background-color: red;
}
::v-deep .el-step__title.is-success {
  color: #fff;
}
::v-deep .el-step__title.is-process {
  color: #ccc !important;
}
.demo-ruleForm {
  width: 500px;
  margin: 0 auto;
  margin-top: 20px;
}
.forms {
  width: 800px;
  margin: 0 auto;
}
::v-deep .el-input__inner {
  background-color: transparent !important;
  color: #fff;
}
::v-deep .el-textarea__inner {
  background-color: transparent !important;
  color: #fff;
}
</style>